package com.bayss.core.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Locale;
import java.util.Map;

@Component
public class SpringContextUtil implements ApplicationContextAware {

    public static ApplicationContext getContext() {
        return context;
    }

    private static ApplicationContext context;

    @SuppressWarnings("static-access")
    @Override
    public void setApplicationContext(ApplicationContext contex)
            throws BeansException {
        this.context = contex;
    }

    public static <T> T getBean(String beanName) {
        return (T) context.getBean(beanName);
    }

    public static <T> T getBean(Class beanType) {
        return (T) context.getBean(beanType);
    }

    public static <T> T getBean(String beanName, Object... args) {
        return (T) context.getBean(beanName, args);
    }


    public static String getMessage(String key) {
        return context.getMessage(key, null, Locale.getDefault());
    }

    public static <T> Map<String, T> getBeansOfType(Class<T> type) {
        return context.getBeansOfType(type);
    }
}
